namespace ITU.EtermCore
{
    using System;
    using System.Text;

    public class CmdPacket : Packet
    {
        public CmdPacket(byte[] b, EtermConnection.CmdResultCB cb) : base(b, cb)
        {
            base.CB = cb;
        }

        public CmdPacket(string cmdstring, EtermConnection.CmdResultCB cb) : base(cmdstring)
        {
            base.CB = cb;
        }

        public CmdPacket(byte[] b, bool is31, EtermConnection.CmdResultCB cb) : base(b, is31, cb)
        {
            base.Is3In1 = true;
            base.CB = cb;
        }

        public CmdPacket(byte[] b, int length, EtermConnection.CmdResultCB cb) : base(b, length)
        {
            base.CB = cb;
        }

        public CmdPacket(int uscid, int cmdid, string cmdstring, EtermConnection.CmdResultCB cb) : this(cmdstring, cb)
        {
        }

        public CmdPacket(string cmdstring, EtermConnection.CmdResultCB cb, CmdResultEvaluator evaluator, CmdPacket subcmdpacket) : this(cmdstring, cb)
        {
            base.SubCmdPacket = subcmdpacket;
            base.Evaluator = evaluator;
        }

        public CmdPacket(string cmdstring, EtermConnection.CmdResultCB cb, bool repeat, CmdResultEvaluator evaluator, CmdPacket subcmdpacket) : this(cmdstring, cb)
        {
            base.Repeat = repeat;
            base.SubCmdPacket = subcmdpacket;
            base.Evaluator = evaluator;
        }

        public CmdPacket(int uscid, int cmdid, string cmdstring, EtermConnection.CmdResultCB cb, CmdResultEvaluator evaluator, CmdPacket subcmdpacket) : this(cmdstring, cb, evaluator, subcmdpacket)
        {
        }

        public CmdPacket(int iusc, int ino, string cmdstring, EtermConnection.CmdResultCB cb, bool repeat, CmdResultEvaluator evaluator, CmdPacket subcmdpacket) : this(cmdstring, cb, repeat, evaluator, subcmdpacket)
        {
        }

        internal override void bulidpacket()
        {
            if (!(base.IsSendByte || base.Is3In1))
            {
                string s = eTermEncode.EncodeCmdString(base.CmdString);
                byte[] bytes = Encoding.ASCII.GetBytes(s);
                base.CmdBytes = new byte[0x15 + bytes.Length];
                Command.StringToBytes("01 00").CopyTo(base.CmdBytes, 0);
                string str2 = base.CmdBytes.Length.ToString("x4");
                Command.StringToBytes(str2.Substring(0, 2) + " " + str2.Substring(2, 2)).CopyTo(base.CmdBytes, 2);
                Command.StringToBytes(string.Format("00 00 00 01 {0} 70 02 1B 0B 20 20 00 0F 1E", Command.BytesToString(base.EcChannel.LoginByte))).CopyTo(base.CmdBytes, 4);
                bytes.CopyTo(base.CmdBytes, 0x13);
                Command.StringToBytes("20 03").CopyTo(base.CmdBytes, (int) (0x13 + bytes.Length));
                base.EncodeCmdBytes = base.CmdBytes;
            }
            else if (!(!base.IsSendByte || base.Is3In1))
            {
                Buffer.BlockCopy(base.EcChannel.LoginByte, 0, base.EncodeCmdBytes, 8, 2);
            }
        }
    }
}
